package tq.com.tqteacher.mine.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.donkingliang.imageselector.utils.ImageSelector;
import com.donkingliang.imageselector.utils.ImageSelectorUtils;

import java.io.File;
import java.util.ArrayList;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqandroidim.TQIM;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseTitleRecycleFragment;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetClient;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.login.TQLoginActivity;
import tq.com.tqteacher.mine.activity.TQClassInfoActivity;
import tq.com.tqteacher.mine.adapter.TQMineAdapter;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQUserInfo;

import static android.app.Activity.RESULT_OK;

public class TQMineFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    public static TQMineFragment newInstance(){
        return new TQMineFragment();
    }

    private TQMineAdapter mMineAdapter;
    private ImageView headerView;
    private TextView userName;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View lView = inflater.inflate(R.layout.fragment_mine_layout, container, false);
        mRecyclerView = lView.findViewById(R.id.recycleView);

        if(mMineAdapter == null){
            mMineAdapter = new TQMineAdapter(this.getContext());
            mMineAdapter.setIClickListener(mClickListener);
        }
        mRecyclerView.setAdapter(mMineAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        headerView = lView.findViewById(R.id.header);

        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                chgHeadeIcon();
            }
        });
        userName = lView.findViewById(R.id.user_name);

//        initData();
        return lView;
    }


    @Override
    public void onStart() {
        super.onStart();
        initData();
    }

    final private TQMineAdapter.IClickListener mClickListener = new TQMineAdapter.IClickListener() {
        @Override
        public void clickItem(int key) {
//            String[] classes = new String[]{
//                    TQClassInfoActivity.class.getName(),
//                    TQClassIntr.class.getName(),
//                    TQClassCardActivity.class.getName(),
//                    TQClassOverInfoActivity.class.getName()
//            };
//
//            int index = key % classes.length;

            Intent intent = new Intent(TQMineFragment.this.getActivity(), TQClassInfoActivity.class);
            intent.putExtra("index", key);
            TQMineFragment.this.getActivity().startActivity(intent);
        }

        @Override
        public void quite() {
            TQUserInfo.cleanSaveInfo(TQMineFragment.this.getContext());
            TQLoginActivity.show(getActivity());
            TQIM.getShareIm().stop();
        }
    };

    private void chgHeadeIcon(){
        //单选
        ImageSelector.builder()
                .useCamera(true) // 设置是否使用拍照
                .setSingle(true)  //设置是否单选
                .setViewImage(true) //是否点击放大图片查看,，默认为true
                .setCrop(true)  // 设置是否使用图片剪切功能。
                .start(this, REQUEST_CODE); // 打开相册
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK){
            return;
        }

        if(requestCode == REQUEST_CODE && data != null){
            ArrayList<String> files = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
            if(files != null && files.size() > 0){
                String pic = files.get(0);

                RequestOptions lOptions = new RequestOptions().circleCrop();
                Glide.with(getContext()).load(new File(pic))
                        .apply(lOptions)
                        .into(headerView);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initData(){
        TQUserInfo lTQUserInfo = TQLogin.getCurrent().getUserInfo();
        userName.setText(lTQUserInfo.getName());

        RequestOptions lOptions = new RequestOptions().error(R.drawable.tx1)
                .placeholder(R.drawable.tx1).circleCrop();
        String url = TQPictureUpload.downloadPath(TQLogin.getCurrent().getUserInfo().getPic());
        Glide.with(this).load(url).apply(lOptions).into(headerView);
    }

    private int REQUEST_CODE = 0x21;
}
